package com.slt.developkitweb.util;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AnnotationTool {

    public static void main(String[] args) {
        String inputFilePath = "/Users/shalongteng/giteeProjects/develop-kit/develop-kit-web/src/main/java/com/slt/developkitweb/util/dto/User.java";
        String outputFilePath = "/Users/shalongteng/giteeProjects/develop-kit/develop-kit-web/src/main/java/com/slt/developkitweb/util/cto/User.java";

        generateFileWithAnnotations(inputFilePath, outputFilePath);
    }

    public static void generateFileWithAnnotations(String inputFilePath, String outputFilePath) {
        Map<String, String> annotations = new HashMap<>();
        StringBuilder contentBuilder = new StringBuilder();

        try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath))) {
            boolean inBlockComment = false;
            StringBuilder currentComment = null;
            String line;
            while ((line = reader.readLine()) != null) {
                contentBuilder.append(line).append("\n");

                if (line.trim().startsWith("/**")) { // 开始多行注释
                    inBlockComment = true;
                    currentComment = new StringBuilder();
                } else if (inBlockComment && line.trim().endsWith("*/")) { // 结束多行注释
                    inBlockComment = false;
                    currentComment.append(line.trim().substring(0, line.trim().length() - 2).trim());
                    processAnnotation(currentComment.toString(), annotations);
                    currentComment = null;
                } else if (inBlockComment) {
                    currentComment.append(line.trim()).append(" ");
                } else if (line.trim().startsWith("//")) { // 单行注释
                    currentComment = new StringBuilder(line.trim().substring(2).trim());
                    processAnnotation(currentComment.toString(), annotations);
                }

                Matcher fieldMatcher = Pattern.compile("(private\\s+\\w+\\s+)(\\w+);").matcher(line);
                if (fieldMatcher.find()) {
                    String fieldName = fieldMatcher.group(2);
                    String annotationText = annotations.get(fieldName);
                    if (annotationText != null) {
                        String annotation = "@ApiModelProperty(\"" + annotationText + "\") ";
                        line = line.replaceFirst(fieldMatcher.group(), annotation + fieldMatcher.group(1));
                    }
                }
                contentBuilder.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 写入新文件
        try {
            Files.write(Paths.get(outputFilePath), contentBuilder.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void processAnnotation(String comment, Map<String, String> annotations) {
        String[] lines = comment.split("\n");
        for (String line : lines) {
            String trimmedLine = line.trim();
            if (!trimmedLine.isEmpty() && !trimmedLine.startsWith("*")) {
                String[] parts = trimmedLine.split("\\s+", 2);
                if (parts.length == 2) {
                    String fieldName = parts[0];
                    String commentText = parts[1];
                    annotations.put(fieldName, commentText);
                }
            }
        }
    }
}